// create by 2017年11月9日20:08:15
var orderStateValue = GetQueryString('orderState');
var shopOrderId = GetQueryString('shopOrderId');
var cancleOrderId = '';
var cancleValue = 1;
var payMoney = '';
var payType = '';
var deliverType = '';
var commentId = '';
var code = '';
var refund = GetQueryString('refund');
$(function () {
    if (refund == 'true') {
        doRefundDetail();
    } else {
        doOrderDetail();
    }
})

function doRefundDetail() {
    $.ajax({
        url: '/purchaseOrderC/refundDetail.json',
        dataType: 'json',
        data: {
            token: getItem('token'),
            goodsOrderId: GetQueryString('goodsOrderId'),
            companyCode:getItem('companyCode'),
        },
        type: 'post',
        beforeSend:function(){
            $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'));
        },
        success: function (data) {
            $('.loading2').hide();
            if (data.code == 1) {
                $('.code2').html('退款类型：' + data.datas.refundLog.refundType);
                $('.state_time').hide();
                $('.state').html(data.datas.refundLog.refundState);
                if(data.datas.refundLog.refundState=='成功退款'){
                    $('.dilever_way').hide()
                }else{
                    $('.dilever_way').html('您已经成功发起退款申请，请耐心等待商家处理');
                }
                $('.addressBox').css({'padding-bottom':'0'});
                $('.addressBox').html('<span style="padding-left: 13px;line-height: 44px;font-size: 14px;">退款信息</span>')
                $('.createTime').html('申请件数：' + data.datas.refundLog.num);
                $('.payType').html('退款原因：' + data.datas.refundLog.refundReason);
                $('.deliverType').html('退款金额：' + data.datas.refundLog.refundMoney);
                $('.pay_way p:nth-child(2n)').css({'padding-bottom':'0','border-bottom':'none'})
                $('.dilever_way').css('color','#232326');
                var orderdetail = data.datas.goods;
                var serviceDetail = data.datas.serviceOrder;
                    $('<div class="goods">\n' +
                        '<div class="detailBox mui-clearfix">\n' +
                        '<div class="goods-img-box">\n' +
                        '<img src="' + orderdetail.goodsImg.split(',')[0] + '">\n' +
                        '</div><div class="goods-product-box">\n' +
                        '<p class="goodsName">' + orderdetail.goodsName + '</p>\n' +
                        '<span class="price">￥' + orderdetail.unitPrice + '<i>×' + orderdetail.goodsNum + '</i></span>\n' +
                        '</div></div><div class="addServiceBox" id="addServiceBox>' +
                        '</div></div>').appendTo($('.goodsBox'));
                for (var b = 0; b < serviceDetail.length; b++) {
                    $('<div class="addService">\n' +
                        '<img class="addSelect" src="../phoneImg/goods_pigeon.jpg" alt="">\n' +
                        '<div class="warranty">' + serviceDetail[b].name + '</div>\n' +
                        '<div class="warranty_price">￥' + serviceDetail[b].price + '</div>\n' +
                        '</div>').appendTo($('#addServiceBox'))
                }
                if(data.datas.refundLog.refundStateValue==1){
                    $('<div class="blue_handle cancleRefund" refundId="'+data.datas.refundLog.id+'" id="cancleRefund" style="background: #fff;color:#e04d45;border: 1px solid #e04d45;border-radius: 0">撤销申请</div>').appendTo($('.handle_box'))
                }
                //撤销申请
                document.getElementById('cancleRefund').addEventListener('tap', function () {
                    var refundId = $(this).attr('refundId');
                    $.ajax({
                        url: '/purchaseOrderC/cancelRefund.json',
                        dataType: 'json',
                        data: {
                            token: getItem('token'),
                            refundId: refundId,
                            companyCode:getItem('companyCode'),
                        },
                        type: 'post',
                        beforeSend: function () {
                            $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'));
                        },
                        success: function (data) {
                            $('.loading2').hide();
                            if (data.code == 1) {
                                mui.alert('撤销成功',function () {
                                    history.go(-1);
                                });
                            }
                            if (data.code == -1) {
                                window.location.href = "login.html"
                            }
                            if (data.code == 0) {
                                mui.toast(data.msg);
                                return;
                            }
                        }
                    })
                })
            }
            if (data.code == -1) {
                window.location.href = "login.html"
            }
            if (data.code == 0) {
                mui.toast(data.msg);
            }
        }
    })
}

function doOrderDetail() {
    $.ajax({
        url: '/purchaseOrderC/orderDetail.json',
        dataType: "json",
        data: {
            shopOrderId: shopOrderId,
            token: getItem('token'),
            companyCode:getItem('companyCode'),
        },
        type: 'post',
        beforeSend:function(){
            $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'));
        },
        success: function (data) {
            $('.loading2').hide();
            if (data.code == 1) {
                $('.code').html(data.shopOrder.code);
                $('.code2').html('订单编号：' + data.shopOrder.code + '<span class="copy">复制</span>');
                var state = '';
                if (data.shopOrder != '创建订单') {
                    $('.state_time').hide();
                }
                $('.state').html(data.shopOrder.orderState);
                if (orderStateValue == 12) {
                    $('.dilever_way').html('您已经成功发起退款申请，请耐心等待商家处理');
                    $('.addressBox').html('<span style="padding-left: 13px">退款信息</span>')
                } else {
                    if (data.shopOrder.deliveryTypeValue == 1) {
                        $('.user_info').html('联系人<span class="name">' + data.shopOrder.shopMan + '</span><span class="phone">' + data.shopOrder.shopPhone + '</span>');
                        $('.address_detail').html(data.shopOrder.shopName+' '+data.shopOrder.shopLocation);
                    } else {
                        $('.name').html(data.deliveryAddress.userName);
                        $('.phone').html(data.deliveryAddress.phone);
                        $('.address_detail').html(data.deliveryAddress.city + data.deliveryAddress.addressDetail);
                    }
                }
                $('.createTime').html('下单时间：' + data.shopOrder.createTime);
                $('.payType').html('支付方式：' + data.shopOrder.payType);
                $('.deliverType').html('配送方式：' + data.shopOrder.deliveryType);
                payType = data.shopOrder.payTypeValue;
                deliverType = data.shopOrder.deliveryTypeValue;
                code = data.deliveryAddress.code;
                payMoney = data.deliveryAddress.payMoney;
                if (data.shopOrder.orderStateValue == 1) {
                    cancleOrderId = data.deliveryAddress.purchaseOrderId;
                } else if (data.shopOrder.orderStateValue == 3) {
                    cancleOrderId = data.shopOrder.id;
                }
                var orderdetail = data.shopOrder.goodsOrders;
                var refund = '';
                commentId = data.shopOrder.id;
                for (var i = 0; i < orderdetail.length; i++) {
                    if (orderdetail[i].orderStateValue == 5) {
                        refund = '<a class="refund" goodsOrderId="' + orderdetail[i].id + '" payMoney="' + orderdetail[i].payMoney + '">退款</a>'
                    } else if (orderdetail[i].orderStateValue == 10) {
                        refund = '<a class="refund" goodsOrderId="' + orderdetail[i].id + '" payMoney="' + orderdetail[i].payMoney + '">申请售后</a>'
                    } else {
                        refund = '';
                    }
                    $('<div class="goods">\n' +
                        '<div class="detailBox mui-clearfix">\n' +
                        '<div class="goods-img-box">\n' +
                        '<img src="' + orderdetail[i].goodsImg.split(',')[0] + '">\n' +
                        '</div><div class="goods-product-box">\n' +
                        '<p class="goodsName">' + orderdetail[i].goodsName + '</p>\n' +
                        '<span class="price">￥' + orderdetail[i].unitPrice + '<i>×' + orderdetail[i].num + '</i><span class="operation_box">' + refund + '</span></span>\n' +
                        '</div></div><div class="addServiceBox" id="addServiceBox' + i + '">' +
                        '</div></div>').appendTo($('.goodsBox'));
                    if(orderdetail[i].serviceOrders.length>0){
                        for (var b = 0; b < orderdetail[i].serviceOrders.length; b++) {
                            $('<div class="addService">\n' +
                                '<img class="addSelect" src="../phoneImg/goods_pigeon.jpg" alt="">\n' +
                                '<div class="warranty">' + orderdetail[i].serviceOrders[b].name + '</div>\n' +
                                '<div class="warranty_price">￥' + orderdetail[i].serviceOrders[b].price + '</div>\n' +
                                '</div>').appendTo($('#addServiceBox' + i + ''))
                        }
                    }
                }
                if (orderStateValue == 1) {
                    $('<div class="blue_handle payOrder">去支付</div>\n' +
                        '<div class="gray_handle" id="cancleOrder">取消订单</div>').appendTo($('.handle_box'))
                } else if (orderStateValue == 3) {
                    $('<div class="gray_handle" id="cancleOrder">取消订单</div>').appendTo($('.handle_box'))
                } else if (orderStateValue == 5) {
                    if (data.shopOrder.hasComment) {
                        $('<div class="blue_handle certainOrder">确认收货</div>' +
                            '<div class="gray_handle commentOrder" href="doComment.html?commentId=' + commentId + '">立即评价</div>').appendTo($('.handle_box'))
                    } else {
                        $('<div class="blue_handle certainOrder">确认收货</div>').appendTo($('.handle_box'))
                    }
                } else if (orderStateValue == 10) {
                    if (data.shopOrder.hasComment) {
                        $('<div class="blue_handle commentOrder" href="doComment.html?commentId=' + commentId + '">立即评价</div>').appendTo($('.handle_box'))
                    }
                }
                //取消订单
                if(orderStateValue == 3||orderStateValue==1){
                    document.getElementById('cancleOrder').addEventListener('tap', function () {
                        showBox('.stage_float_box', 200);
                        document.getElementById('close_stage').addEventListener('tap', function () {
                            hideBox('.stage_float_box', 100);
                        })
                        document.getElementById('stages_transaction').addEventListener('tap', function () {

                            if(orderStateValue==1){
                                $.ajax({
                                    url: '/purchaseOrderC/cancelOrder.json',
                                    dataType: 'json',
                                    data: {
                                        token: getItem('token'),
                                        purchaseOrderId: cancleOrderId,
                                        cancelReasonValue: cancleValue,
                                        companyCode:getItem('companyCode'),
                                    },
                                    type: 'post',
                                    beforeSend: function () {
                                        $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'));
                                    },
                                    success: function (data) {
                                        $('.loading2').hide();
                                        if (data.code == 1) {
                                            mui.toast('取消订单成功');
                                            window.location.href="order.html";
                                            hideBox('.stage_float_box', 100);
                                        }
                                        if (data.code == -1) {
                                            window.location.href = "login.html"
                                        }
                                        if (data.code == 0) {
                                            mui.toast(data.msg);
                                            return;
                                        }
                                    }
                                })
                            }else if(orderStateValue==3){
                                $.ajax({
                                    url: '/purchaseOrderC/stopOrder.json',
                                    dataType: 'json',
                                    data: {
                                        token: getItem('token'),
                                        shopOrderId: cancleOrderId,
                                        cancelReasonValue: cancleValue,
                                        companyCode:getItem('companyCode'),
                                    },
                                    type: 'post',
                                    beforeSend: function () {
                                        $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'));
                                    },
                                    success: function (data) {
                                        $('.loading2').hide();
                                        if (data.code == 1) {
                                            mui.toast('取消订单成功');
                                            window.location.href="order.html";
                                            hideBox('.stage_float_box', 100);
                                        }
                                        if (data.code == -1) {
                                            window.location.href = "login.html"
                                        }
                                        if (data.code == 0) {
                                            mui.toast(data.msg);
                                            return;
                                        }
                                    }
                                })
                            }

                        })
                    })
                }

            }
            if (data.code == -1) {
                window.location.href = "login.html"
            }
            if (data.code == 0) {
                mui.toast(data.msg);
            }
        }
    })
}

//取消订单
$('.state_detail_img img').click(function () {
    if ($(this).attr('src') == '../phoneImg/no_pigeon.jpg') {
        $('.state_detail_img img').attr('src', '../phoneImg/no_pigeon.jpg');
        $(this).attr('src', '../phoneImg/goods_pigeon.jpg');
        cancleValue = $(this).attr('cancleValue');
    }
})

//支付
mui('body').on('tap', '.payOrder', function () {
    setItem('deliveryType', deliverType);
    setItem('payType', payType);
    window.location.href="choosePayWay.html?payMoney="+payMoney+'&code='+code;

})

//退款
$(document).on('click', '.refund', function () {
    var goodsInfo = {};
    goodsInfo.goodsImg = $(this).parent().parent().parent().siblings('.goods-img-box').find('img').attr('src');
    goodsInfo.guige = $(this).attr('guige');
    goodsInfo.goodsName = $(this).parent().parent().parent().find('.goodsName').html();
    window.location.href = "refund.html?goodsOrderId=" + $(this).attr('goodsOrderId') + '&payMoney=' + $(this).attr('payMoney') + '&goodsInfo=' + encodeURI(JSON.stringify(goodsInfo));
})

//评价
mui('body').on('tap', '.commentOrder', function () {
    window.location.href = $(this).attr('href');
})


//确认收货
mui('body').on('tap', '.certainOrder', function () {
    var cancleOrderId = $(this).attr('certainId');
    var btnArray = ['取消', '确定'];
    mui.confirm('请确认已收到货之后再确认订单？', '', btnArray, function (e) {
        if (e.index == 1) {
            $.ajax({
                url: '/purchaseOrderC/confirmReceiptOrder.json',
                dataType: 'json',
                data: {
                    token: getItem('token'),
                    shopOrderId: commentId,
                    companyCode:getItem('companyCode'),
                },
                type: 'post',
                beforeSend: function () {
                    $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'));
                },
                success: function (data) {
                    $('.loading2').hide();
                    if (data.code == 1) {
                        mui.toast('确认收货成功');
                        window.location.href="order.html?orderStateValue=12";
                    }
                    if (data.code == -1) {
                        window.location.href = "login.html"
                    }
                    if (data.code == 0) {
                        mui.toast(data.msg);
                        return;
                    }
                }
            })
        } else {

        }
    })
})

//复制
mui('body').on('tap', '.copy', function () {
    //var Url2=document.getElementById("biao1");
    var Url2 = $(this).siblings('.code');
    Url2.select(); // 选择对象
    document.execCommand("Copy"); // 执行浏览器复制命令
    mui.toast('已复制')
})

mui('.mui-scroll-wrapper').scroll({
    deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
});



